package com.hzy.servlet;

import com.hzy.pojo.Rent;
import com.hzy.service.RentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;


/**
 * @Package: servlet
 * @ClassName: AddServlet
 * @Author: 862982795
 * @CreateTime: 2021/06/02 10:50
 * @Description:
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        RentService service = new RentService();

        String userName = request.getParameter("userName");
        String carNum = request.getParameter("carNum");
        String maturityDate = (request.getParameter("maturityDate"));

        Rent rent = new Rent();
        rent.setUserName(userName);
        rent.setCarNum(carNum);
        rent.setMaturityDate(Date.valueOf(maturityDate));
        rent.setStatus(0);

        int i = service.add(rent);

        PrintWriter out = response.getWriter();
        if (i > 0) {
            out.println("<script>alert('添加成功');location.href='ListServlet';</script>");
        } else {
            out.println("<script>alert('添加失败!');history.back();</script>");
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
